const { Todo } = require('../controller')
const { useObjectIds, useForm } = require('../middleware')

const routes = [
    {
        path: '/router',
        description: '父路由',
        method: 'get',
        // 父路由中间件，只作用于children
        use: (req, res, next) => {
            console.log('经过父路由')
            req.from = '来自父路由'
            next()
        },
        children: [
            {
                path: '/router/path',
                description: '子路由',
                method: 'get',
                use: (req, res) => {res.json({ code: 200, tip: '请控制台看打印', from: req.from })}
            }
        ]
    },
    {
        path: '/todo',
        description: '演示数据库的增删查改',
        children: [
            {
                path: '/todo/add',
                description: '数据库插入1条数据',
                method: 'post',
                use: [useObjectIds('todoId'), Todo.insertOne.bind(Todo)]
            },
            {
                path: '/todo/remove',
                description: '数据库删除1条数据',
                method: 'delete',
                use: Todo.deleteOne.bind(Todo)
            },
            {
                path: '/todo/list',
                description: '查询数据库数据',
                method: 'get',
                use: Todo.getAll.bind(Todo)
            },
            {
                path: '/todo/update',
                description: '修改数据库数据',
                method: 'put',
                use: Todo.updateOne.bind(Todo)
            }
        ]
    },
    {
        path: '/uploadFile',
        description: '上传文件材料,content-type必须是multipart/form-data',
        method: 'post',
        use: [useForm('./server/file'), (req, res) => res.json({ code: 200, paths: req.body.fileList })]
    }
]

module.exports = {
    routes
}
